package com.android.kaui;
/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.android.kaui.R;
import com.android.kaui.MainSwingUI.SampleView;
import com.android.kaui.ext.KauiManager;
import com.android.kaui.ext.KauiTabActivity;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.content.Intent;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TabWidget;




/**
 * An example of tabs that uses labels ({@link TabSpec#setIndicator(CharSequence)})
 * for its indicators and views by id from a layout file ({@link TabSpec#setContent(int)}).
 */
public class KauiTab extends KauiTabActivity {
	public final static String TAG = "KauiTab";
	public final static String EXTRA_LISTKEY = "listkey";
	public final static String EXTRA_ACTIVEKEY = "activekey";
	
	public static TabHost tabHost;
	
	int currentIndex;
	public static boolean isTabActive = false;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.d(TAG, "onCreate.........");
        //tab init
        tabHost = getTabHost();
        LayoutInflater.from(this).inflate(R.layout.grid_1, 
        		tabHost.getTabContentView(), true);
        
        Log.d(TAG, "getTabHost end.........");
        
      //get intent list array
	 Bundle extras = getIntent().getExtras();
	 
	 Log.d(TAG, "extras = " + extras);
	 
	 if(extras !=null)
	 {
		 int array[] = extras.getIntArray(EXTRA_LISTKEY);
		 int active = extras.getInt(EXTRA_ACTIVEKEY);
		 for(int i=0;i<array.length;i++)
			 Log.d("Tabs", "array["+i+"] = "+super.manager.getIntentTitle(array[i])); //OK!~~~~~~~
	 	
			 tabactivelist(active, array);
			 
			 KauiTab.isTabActive = true;
			 
//			 tabHost.clearAllTabs();
//			 getTabWidget().setEnabled(false);
	 }
    }
    
	
	public void setCurrentTab(int _index)
	{
		tabHost.setCurrentTab(_index);
	}
	
	   
    public void tabactivelist(int _active, int array[])
    {
    	int len = array.length;
    	int i, index;
    	
    	String strTabSpec, strIndi;
    	Intent intent;
    	
    	currentIndex = 0;

    	int sortArray[] = tabSort(array, _active);
    	
    	for(i=0;i<len;i++)
    	{
    		index = sortArray[i];
    		
    		if(super.manager.isTabIntent(index)) {
	    		strTabSpec = super.manager.getIntentTitle(index);
	    		strIndi = super.manager.getIntentTitle(index);
	    		intent = super.manager.returnIntent(index);
	    		add(index, strTabSpec, strIndi, intent);
    		}
    	}
    	
    	for(i=0;i<array.length;i++)
			Log.d(TAG, "SampleView.array["+i+"] = " +array[i] );
		
    	
    	setCurrentTab(currentIndex);
    	
    	Log.d(TAG, "tabactivelist end.....");
    }
    
    private int[] tabSort(int[] _array, int _active) {
			ArrayList<Integer> array = new ArrayList<Integer>();
			int len=_array.length;
			int actindex = -1;
			
			//active index seach
			for(int i=0;i<len;i++) {
				array.add(_array[i]);
				if(_array[i] == _active)
				{
					actindex = i;
				}
			}
			len = array.size();
			
			//active index -> first index
			if(array.contains(_active))
			{
				//array.remove(actindex);
				currentIndex = actindex;
			}
			else 
			{
				array.add(_active);
				len++;
				currentIndex = len;
			}
			
			//make array
			int rArray[] = new int[len];
			for(int i=0;i<len;i++)
			{
				rArray[i] = array.get(i);
			}
			
			return rArray;
	}


	public void add(int idx, String tabSpec, String tabIndi, Intent intents)
    {
    	tabHost.addTab(tabHost.newTabSpec(tabSpec)
                .setIndicator(tabIndi,getResources().getDrawable(SampleView.iconImageR[idx]))
                .setContent(intents));
    }
	
	
	public void addListIndex(int _index)
	{
		
	}
}
